package com.ruoyi.project.dict.res.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.material.service.IMaterialService;
import com.ruoyi.project.dict.res.domain.MaterialRes;
import com.ruoyi.project.dict.res.service.IMaterialResService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学习资料资源Controller
 *
 * @author shixiangwen
 * @date 2020-05-08
 */
@Controller
@RequestMapping("/dict/res")
public class MaterialResController extends BaseController {

    private String prefix = "dict/res";

    @Autowired
    private IMaterialResService materialResService;

    @Autowired
    private IMaterialService materialService;

    @RequiresPermissions("dict:res:view")
    @GetMapping()
    public String res() {
        return prefix + "/res";
    }

    @RequiresPermissions("yingyu:activityReg:view")
    @GetMapping("/{materialId}")
    public String res(@PathVariable("materialId") Long materialId, ModelMap mmap) {
        mmap.put("material", materialService.selectMaterialById(materialId));
        return prefix + "/res";
    }

    /**
     * 查询学习资料资源列表
     */
    @RequiresPermissions("dict:res:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MaterialRes materialRes) {
        startPage();
        List<MaterialRes> list = materialResService.selectMaterialResList(materialRes);
        return getDataTable(list);
    }

    /**
     * 导出学习资料资源列表
     */
    @RequiresPermissions("dict:res:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MaterialRes materialRes) {
        List<MaterialRes> list = materialResService.selectMaterialResList(materialRes);
        ExcelUtil<MaterialRes> util = new ExcelUtil<MaterialRes>(MaterialRes.class);
        return util.exportExcel(list, "res");
    }

    /**
     * 新增学习资料资源
     *
     * @param topicId
     * @param mmap
     * @return
     */
    @GetMapping("/add/{materialId}")
    public String add(@PathVariable("materialId") Long materialId, ModelMap mmap) {
        mmap.put("materialId", materialId);
        mmap.put("material", materialService.selectMaterialById(materialId));
        return prefix + "/add";
    }

    /**
     * 新增保存学习资料资源
     */
    @RequiresPermissions("dict:res:add")
    @Log(title = "学习资料资源", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MaterialRes materialRes) {
        return toAjax(materialResService.insertMaterialRes(materialRes));
    }

    /**
     * 修改学习资料资源
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        MaterialRes materialRes = materialResService.selectMaterialResById(id);
        mmap.put("material", materialService.selectMaterialById(materialRes.getMaterialId()));
        mmap.put("materialRes", materialRes);
        return prefix + "/edit";
    }

    /**
     * 修改保存学习资料资源
     */
    @RequiresPermissions("dict:res:edit")
    @Log(title = "学习资料资源", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MaterialRes materialRes) {
        return toAjax(materialResService.updateMaterialRes(materialRes));
    }

    /**
     * 删除学习资料资源
     */
    @RequiresPermissions("dict:res:remove")
    @Log(title = "学习资料资源", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(materialResService.deleteMaterialResByIds(ids));
    }
}
